package cn.iocoder.yudao.module.erp.controller.admin.purchaseplan;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.PurchasePlanPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.PurchasePlanRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.PurchasePlanSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseengineeringdetail.PurchaseEngineeringDetailExportRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseengineeringdetail.PurchaseEngineeringDetailExportVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseequipmentdetail.PurchaseEquipmentDetailExportRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseequipmentdetail.PurchaseEquipmentDetailExportVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchasematerialdetail.PurchaseMaterialDetailExportRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchasematerialdetail.PurchaseMaterialDetailExportVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseminordetail.PurchaseMinorDetailExportRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseminordetail.PurchaseMinorDetailExportVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseservicedetail.PurchaseServiceDetailExportRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseservicedetail.PurchaseServiceDetailExportVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchaseplan.PurchasePlanDO;
import cn.iocoder.yudao.module.erp.service.purchaseplan.PurchasePlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 采购计划")
@RestController
@RequestMapping("/erp/purchase-plan")
@Validated
public class PurchasePlanController {

    @Resource
    private PurchasePlanService purchasePlanService;

    @GetMapping("/page")
    @Operation(summary = "获得采购计划分页")
    @PreAuthorize("@ss.hasPermission('erp:purchase-plan:query')")
    public CommonResult<PageResult<PurchasePlanRespVO>> getPurchasePlanPage(@Valid PurchasePlanPageReqVO pageReqVO) {
        PageResult<PurchasePlanDO> pageResult = purchasePlanService.getPurchasePlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PurchasePlanRespVO.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建采购计划")
    @PreAuthorize("@ss.hasPermission('erp:purchase-plan:create')")
    public CommonResult<Long> createPurchasePlan(@Valid @RequestBody PurchasePlanSaveReqVO createReqVO) {
        return success(purchasePlanService.createPurchasePlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新采购计划")
    @PreAuthorize("@ss.hasPermission('erp:purchase-plan:update')")
    public CommonResult<Boolean> updatePurchasePlan(@Valid @RequestBody PurchasePlanSaveReqVO updateReqVO) {
        purchasePlanService.updatePurchasePlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除采购计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:purchase-plan:delete')")
    public CommonResult<Boolean> deletePurchasePlan(@RequestParam("id") Long id) {
        purchasePlanService.deletePurchasePlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除采购计划")
                @PreAuthorize("@ss.hasPermission('erp:purchase-plan:delete')")
    public CommonResult<Boolean> deletePurchasePlanList(@RequestParam("ids") List<Long> ids) {
        purchasePlanService.deletePurchasePlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取详情接口")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:purchase-plan:query')")
    public CommonResult<PurchasePlanRespVO> getPurchasePlan(@RequestParam("id") Long id) {
        return success(purchasePlanService.getPurchasePlan(id));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出采购计划 Excel")
    @PreAuthorize("@ss.hasPermission('erp:purchase-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPurchasePlanExcel(@Valid PurchasePlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PurchasePlanDO> list = purchasePlanService.getPurchasePlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "采购计划.xlsx", "数据", PurchasePlanRespVO.class,
                        BeanUtils.toBean(list, PurchasePlanRespVO.class));
    }




    @GetMapping("/exportExcelTemplate/service")
    @Operation(summary = "导出服务采购明细模板")
    @PermitAll
    public void exportServiceDetailExcelTemplate(HttpServletResponse response) throws IOException {
        // 设置响应头，确保浏览器直接下载文件
        // 导出 Excel
        ExcelUtils.appWrite(response, "服务采购明细模板.xlsx", "数据", PurchaseServiceDetailExportVO.class,
                BeanUtils.toBean(new ArrayList<>(), PurchaseServiceDetailExportVO.class));
    }

    @PostMapping("/import/service")
    @Operation(summary = "导入服务采购明细")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
    })
    @PermitAll
    public CommonResult<PurchaseServiceDetailExportRespVO> importServiceDetail(@RequestParam("file") MultipartFile file) throws Exception {
        List<PurchaseServiceDetailExportVO> list = ExcelUtils.read(file, PurchaseServiceDetailExportVO.class);
        list = list.stream().filter(x-> StrUtil.isNotBlank(x.getPurchaseContent())).collect(Collectors.toList());
        return success(purchasePlanService.importServiceDetailList(list));
    }




    @GetMapping("/exportExcelTemplate/equipment")
    @Operation(summary = "导出设备采购明细模板")
    @PermitAll
    public void exportEquipmentDetailExcelTemplate(HttpServletResponse response) throws IOException {
        // 设置响应头，确保浏览器直接下载文件
        // 导出 Excel
        ExcelUtils.appWrite(response, "设备采购明细模板.xlsx", "数据", PurchaseEquipmentDetailExportVO.class,
                BeanUtils.toBean(new ArrayList<>(), PurchaseEquipmentDetailExportVO.class));
    }


    @PostMapping("/import/equipment")
    @Operation(summary = "导入设备采购明细")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
    })
    @PermitAll
    public CommonResult<PurchaseEquipmentDetailExportRespVO> importEquipmentDetail(@RequestParam("file") MultipartFile file) throws Exception {
        List<PurchaseEquipmentDetailExportVO> list = ExcelUtils.read(file, PurchaseEquipmentDetailExportVO.class);
        list = list.stream().filter(x-> StrUtil.isNotBlank(x.getEquipmentName())).collect(Collectors.toList());
        return success(purchasePlanService.importEquipmentDetailList(list));
    }




    @GetMapping("/exportExcelTemplate/engineering")
    @Operation(summary = "导出工程采购明细模板")
    @PermitAll
    public void exportEngineeringDetailExcelTemplate(HttpServletResponse response) throws IOException {
        // 设置响应头，确保浏览器直接下载文件
        // 导出 Excel
        ExcelUtils.appWrite(response, "工程采购明细模板.xlsx", "数据", PurchaseEngineeringDetailExportVO.class,
                BeanUtils.toBean(new ArrayList<>(), PurchaseEngineeringDetailExportVO.class));
    }

    @PostMapping("/import/engineering")
    @Operation(summary = "导入工程采购明细")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
    })
    @PermitAll
    public CommonResult<PurchaseEngineeringDetailExportRespVO> importEngineeringDetail(@RequestParam("file") MultipartFile file) throws Exception {
        List<PurchaseEngineeringDetailExportVO> list = ExcelUtils.read(file, PurchaseEngineeringDetailExportVO.class);
        list = list.stream().filter(x-> StrUtil.isNotBlank(x.getPurchaseContent())).collect(Collectors.toList());
        return success(purchasePlanService.importEngineeringDetailList(list));
    }




    @GetMapping("/exportExcelTemplate/material")
    @Operation(summary = "导出物资采购明细模板")
    @PermitAll
    public void exportMaterialDetailExcelTemplate(HttpServletResponse response) throws IOException {
        // 设置响应头，确保浏览器直接下载文件
        // 导出 Excel
        ExcelUtils.appWrite(response, "物资采购明细模板.xlsx", "数据", PurchaseMaterialDetailExportVO.class,
                BeanUtils.toBean(new ArrayList<>(), PurchaseMaterialDetailExportVO.class));
    }

    @PostMapping("/import/material")
    @Operation(summary = "导入物资采购明细")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
    })
    @PermitAll
    public CommonResult<PurchaseMaterialDetailExportRespVO> importMaterialDetail(@RequestParam("file") MultipartFile file) throws Exception {
        List<PurchaseMaterialDetailExportVO> list = ExcelUtils.read(file, PurchaseMaterialDetailExportVO.class);
        list = list.stream().filter(x-> StrUtil.isNotBlank(x.getMaterialName())).collect(Collectors.toList());
        return success(purchasePlanService.importMaterialDetailList(list));
    }




    @GetMapping("/exportExcelTemplate/minor")
    @Operation(summary = "导出直接类、小额零星采购明细模板")
    @PermitAll
    public void exportMinorDetailExcelTemplate(HttpServletResponse response) throws IOException {
        // 设置响应头，确保浏览器直接下载文件
        // 导出 Excel
        ExcelUtils.appWrite(response, "直接类采购明细模板.xlsx", "数据", PurchaseMinorDetailExportVO.class,
                BeanUtils.toBean(new ArrayList<>(), PurchaseMinorDetailExportVO.class));
    }

    @PostMapping("/import/minor")
    @Operation(summary = "导入直接类、小额零星采购明细")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
    })
    @PermitAll
    public CommonResult<PurchaseMinorDetailExportRespVO> importMinorDetail(@RequestParam("file") MultipartFile file) throws Exception {
        List<PurchaseMinorDetailExportVO> list = ExcelUtils.read(file, PurchaseMinorDetailExportVO.class);
        list = list.stream().filter(x-> StrUtil.isNotBlank(x.getItemName())).collect(Collectors.toList());
        return success(purchasePlanService.importMinorDetailList(list));
    }


}